package com.subfg.payment.aspect.util;

import com.alibaba.fastjson.JSONObject;
import com.subfg.common.entity.common.BatchRecordModel;
import com.subfg.common.entity.common.CommonHeadModel;
import com.subfg.common.entity.common.CommonParam;
import com.subfg.common.entity.common.CommonResult;
import com.subfg.common.entity.genter.zlog.ZlogRequestModel;
import com.subfg.common.rabbitmq.constant.ExchangeApi;
import com.subfg.common.rabbitmq.constant.QueueContentModel;
import com.subfg.common.rabbitmq.produer.MQSender;
import com.subfg.common.util.auth.GenerateId;
import com.subfg.common.util.common.StringUtil;
import com.subfg.payment.constant.UriDesciptionEnum;
import com.subfg.payment.rabiitmq.constant.RoutingKeyApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class ResultAspectUtil {
    private Logger log = LoggerFactory.getLogger(ResultAspectUtil.class);
    // 自定义异常类
    private static final String EXCEPETIONCLASSNAMEMODEL = "com.subfg.common.constant.BusinessExceptionModel";
    // 参数回调类
    private static final String PAYMENTCALLBACK = "com.subfg.payment.controller.noauth.PaymentNotifyCall";

    @Autowired
    private MQSender mqSender;
    @Autowired
    private GenerateId generateId;


    public boolean exceptionClassJudgeVerfiy(String exceptionClassName) {
        if (EXCEPETIONCLASSNAMEMODEL.equals(exceptionClassName)) {
            return true;
        }
        return false;
    }

    /**
     * 判断类名是否需要拦截, 无需加解密处理
     * @param className  回调接口类名,
     * @return
     */
    public boolean payCallBackClassVeify(String className) {
        if (PAYMENTCALLBACK.equals(className)) {
            return true;
        }
        return false;
    }


    public void insertActionDevice(CommonResult res, String requestParam,
                                   UriDesciptionEnum uriDesciptionEnum, String userIp,
                                   HttpServletRequest request, Integer differe, Long startRequestTime) {
        byte num = 0;
        if (res.getCode() >= 0) {
            num = 1;
        }
        ZlogRequestModel systemLog = new ZlogRequestModel();
        if (differe == 1) {
            systemLog = new ZlogRequestModel().builder()
                    .actionStatus(num)
                    .userIp(userIp)
                    .pageName("三方回调请求")
                    .param(StringUtil.replace(requestParam))
                    .resultParam(StringUtil.replace(JSONObject.toJSONString(res)))
                    .message(res.getMessage())
                    .uri(uriDesciptionEnum.getApiName())
                    .actionName(uriDesciptionEnum.getMessage())
                    .createTime(startRequestTime)
                    .build();
        } else {
            CommonParam param = JSONObject.parseObject(requestParam, CommonParam.class);
            CommonHeadModel commonHeadModel = param.getCommon();
            systemLog = new ZlogRequestModel().builder()
                    .actionStatus(num)
                    .userIp(userIp)
                    .requestMethod(commonHeadModel.getOsType())
                    .pageName(commonHeadModel.getPageName())
                    .createTime(startRequestTime)
                    .uri(uriDesciptionEnum.getApiName())
                    .actionName(uriDesciptionEnum.getMessage())
                    .param(StringUtil.replace(requestParam))
                    .resultParam(StringUtil.replace(JSONObject.toJSONString(res)))
                    .message(res.getMessage())
                    .build();
        }
        Long timeConsuming = System.currentTimeMillis() - startRequestTime;
        systemLog.setTimeConsuming(timeConsuming);
        // 获取日志表名
        String tableName = "zlog_request";
        BatchRecordModel<ZlogRequestModel> batchRecordModel = new BatchRecordModel<ZlogRequestModel>(tableName, systemLog);
        mqSender.sendMQMessage(ExchangeApi.EXCHANGE_COMMON, RoutingKeyApi.SYSTEM_LOGGER,
                new QueueContentModel<BatchRecordModel>(generateId.genrateMqId(), 0, batchRecordModel));
        log.info("此次接口耗时==>>>{}毫秒,返回数据成功==>>>{}!!请求url地==>>>{}!!!",timeConsuming, systemLog.getRequestMethod(), uriDesciptionEnum.getApiName());
    }

}
